package fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import mindshare.JSONParser;
import mindshare.MainActivity;
import info.androidhive.slidingmenu.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class HomeFragment extends Fragment {
	
	public HomeFragment(){}
	
	JSONParser jParser = new JSONParser();
	public ArrayList<String> countriesList= new ArrayList<String>();
//	private static String url = "http://10.48.100.213:8888/mindShare/obtainCities.php";		//Try with your ip address
	private static String url = "http://192.168.1.196:8888/mindShare/obtainCities.php";
	private ProgressDialog pDialog;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View rootView = inflater.inflate(R.layout.fragment_home, container, false);
		Button b = (Button) rootView.findViewById(R.id.btnGetCities);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
            	Context c= rootView.getContext();
            	LoadCities load= new LoadCities(c);
            	load.execute();
            }
        });
        return rootView;
    }

	class LoadCities extends AsyncTask<String, String, String> {
		Context c;
		
		LoadCities(Context context){
			c = context;
		}
		
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(c);
            pDialog.setMessage("Loading cities. Please wait...");
            pDialog.show();
        }
 
		protected String doInBackground(String... args) {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("country", "Argentina"));
			JSONObject json = jParser.makeHttpRequest(url, params);
			JSONArray countries;
			countriesList.clear();
			try {
				countries = json.getJSONArray("countries");
				for (int i = 0; i < countries.length(); i++) {
					String c = countries.getString(i);
					countriesList.add(c);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		
        protected void onPostExecute(String file_url) {
        	TextView tvOutput = (TextView) ((Activity)c).findViewById(R.id.tvOutput);
        	tvOutput.setText("Countries: ");
    		for (int i=0; i<countriesList.size(); i++){
    			tvOutput.append(countriesList.get(i)+" ");
    		}
            pDialog.dismiss();
        }
    }
}
